//
//  LocalViewController.m
//  哎游
//
//  Created by 林程杰 on 15/1/13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LocalViewController.h"
#import "registerViewController.h"
#import "sinaWiboViewController.h"
#import "UserDataBase.h"
#import "Singleton.h"

@implementation LocalViewController
- (void)dealloc
{
    [_field release];
    [_fieldPas release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [self creatView];
}

- (void) creatView
{
    UIImageView *imageBig = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageBig.image = [UIImage imageNamed:@"14.png"];
    imageBig.userInteractionEnabled = YES;
    [self.view addSubview:imageBig];
    [imageBig release];
    
    
    self.field = [[UITextField alloc] initWithFrame:CGRectMake( 50, self.view.frame.size.height / 3, self.view.frame.size.width - 100, 27)];
    _field.text = @"登陆账号/哎游账号";
    _field.textColor = [UIColor grayColor];
    _field.clearsOnBeginEditing = YES;
    [self.view addSubview:_field];
    
    
    
//    self.fieldPas = [[UITextField alloc] initWithFrame:CGRectMake( 50, self.view.frame.size.height / 3 + 33, self.view.frame.size.width - 100, 27)];
//    _fieldPas.textColor = [UIColor grayColor];
//    _fieldPas.clearsOnBeginEditing = YES;
//    //_fieldPas.placeholder = @"密码";
//    _fieldPas.text = @"密码1";
//    _fieldPas.secureTextEntry = YES;
//    [self.view addSubview:_fieldPas];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, self.view.frame.size.height / 3 + 25, self.view.frame.size.width - 100, 1)];
    [label setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:label];
    [label release];
    

    self.fieldPas = [[UITextField alloc] initWithFrame:CGRectMake( 50, self.view.frame.size.height / 3 + 33, self.view.frame.size.width - 100, 27)];
    _fieldPas.text = @"密码";
    _fieldPas.textColor = [UIColor grayColor];
    _fieldPas.clearsOnBeginEditing = YES;
    _fieldPas.delegate = self;
    [self.view addSubview:_fieldPas];
    

    UILabel *labelPas = [[UILabel alloc] initWithFrame:CGRectMake( 50, self.view.frame.size.height / 3 + 58, self.view.frame.size.width - 100, 1)];
    [labelPas setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:labelPas];
    [labelPas release];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, self.view.frame.size.height / 3 + 80, self.view.frame.size.width - 100, 35)];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [button setBackgroundColor:[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:100]];
    [button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button release];
    
    UIButton *buttonLeft = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 25, 25)];
    [buttonLeft setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    [self.view addSubview:buttonLeft];
    [buttonLeft addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];
    [buttonLeft release];
    
    UIButton *buttonRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width- 50, 20, 40, 25)];
    [buttonRight setTitle:@"注册" forState:UIControlStateNormal];
    [buttonRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonRight addTarget:self action:@selector(registerPage :) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonRight];
    [buttonRight release];
    [_fieldPas release];
    [_field release];
}

//返回按钮
- (void) returnBack
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES;
}

//进入注册界面
- (void) registerPage : (id) sender
{
    registerViewController *registerView = [[registerViewController alloc] init];
    UINavigationController *na = [[UINavigationController  alloc] initWithRootViewController:registerView];
    [self presentViewController:na animated:YES completion:nil];

    [registerView release];
    [na release];
}

- (void)login : (id) sender
{
    BOOL isExist = [UserDataBase selectForUserName:_field.text passWord:_fieldPas.text];
    
    if (isExist == YES) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"登录成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:@"yes" forKey:@"login"];
        
        [user setObject:_field.text forKey:@"userName"];
        Singleton *share = [Singleton shareInstance];
        share.loginState = LocalLogin;
        [user setValue:@"local" forKey:@"state"];
        
        //进入收藏界面
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"账号或者密码错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    } 
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_field resignFirstResponder];
    [_fieldPas resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
 _fieldPas.secureTextEntry = YES;
}


@end
